package com.jiukuaidao.zhulin.module.address;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;

import com.jiukuaidao.library.base.BaseActivity;
import com.jiukuaidao.library.util.ClearEditText;
import com.jiukuaidao.library.widget.recyclerview.XRecyclerView;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.adapter.GainPlaceAdapter;
import com.jiukuaidao.zhulin.data.TaskProvider;
import com.jiukuaidao.zhulin.module.address.bean.PreAddressBean;
import com.jiukuaidao.zhulin.module.address.contract.GainPlaceContract;
import com.jiukuaidao.zhulin.module.address.presenter.GainPlacePresenter;
import com.jiukuaidao.zhulin.util.AddressInfoUtil;
import com.ta.utdid2.android.utils.StringUtils;

import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Author : PaiTaSuo
 * Date : 2016/5/31
 * Description :
 */
public class GainPlaceActivity extends BaseActivity<GainPlaceContract.Presenter> implements GainPlaceContract.View, View.OnClickListener {
    @BindView(R.id.titile_left_imageview)
    ImageView mLeftImg;
    @BindView(R.id.et_search)
    ClearEditText mEtSearch;
    @BindView(R.id.place_recycle)
    XRecyclerView mPlaceRecycle;
    private PreAddressBean mAddressInfo;

    private GainPlaceAdapter mBaseAdapter;
    private String mRegion;
    private String mCityCode;
    private String mPreLongitude;
    private String mPreLatitude;
    private String mPreCityCode;
    private String adapterType = "gain";

    @Override
    protected View getTargetView() {
        return null;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_gain_place;
    }

    @Override
    protected void handleBundle(Bundle bundle) {
        mRegion = bundle.getString("region", "");
        mCityCode = bundle.getString("cityCode", "");
    }

    @Override
    protected void init() {
        setOnClickListener();
        mAddressInfo = AddressInfoUtil.getAddressInfo();
        mPreLongitude = mAddressInfo.longitude;
        mPreLatitude = mAddressInfo.latitude;
        mPreCityCode = mAddressInfo.cityCode;

        mPlaceRecycle.setLoadingMoreEnabled(false);
        mPlaceRecycle.setPullRefreshEnabled(false);

        new GainPlacePresenter(TaskProvider.provideTasksRepository(this), this);
        if (mPreCityCode.equals(mCityCode) && !StringUtils.isEmpty(mPreLongitude) && !StringUtils.isEmpty(mPreLatitude)) {
            mPresenter.startGeoCoderSearch();
        }
        mEtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!StringUtils.isEmpty(s.toString())) {
                    mPresenter.startSuggestionSearch();  // 根据关键字搜索
                } else {
                    if (mPreCityCode.equals(mCityCode) && !StringUtils.isEmpty(mPreLongitude) && !StringUtils.isEmpty(mPreLatitude)) {
                        mPresenter.startGeoCoderSearch(); //根据经纬度
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    public double getLongitude() {
        return Double.parseDouble(mPreLongitude);
    }

    @Override
    public double getLatitude() {
        return Double.parseDouble(mPreLatitude);
    }

    @Override
    public String getQuery() {
        return mEtSearch.getText().toString();
    }

    @Override
    public String getRegion() {
        return mRegion;
    }

    @Override
    public String getCityCode() {
        return mCityCode;
    }

    @Override
    public void placeList(final List<Map<String, String>> list, final String type) {
        mBaseAdapter = new GainPlaceAdapter(this, R.layout.item_search_place, list, mPlaceRecycle, type, adapterType);
        mPlaceRecycle.setLayoutManager(new LinearLayoutManager(this));
        mPlaceRecycle.setAdapter(mBaseAdapter);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titile_left_imageview:
                finish();
                break;
        }
    }

    public void setOnClickListener() {
        mLeftImg.setOnClickListener(this);
    }
}
